var invoice_item_selection;
function updateInvoiceRowTotal(item){
	var parent = $(item).parent().parent();
	var qty = Number($('.quantity',parent).val());
	var rate= Number($('.rate',parent).val());
	$('.itmtotal',parent).val(qty*rate);
	updateInvoiceSubTotals();
}

function updateInvoiceSubTotals(){
	var total=0;
	$('.itmtotal').each(function(index,el){
		var val=Number($(this).val());
		if(!isNaN(val)){
			total+=val;
		} else {
			$(this).val("ERROR");
		}
	});
	var discount = Number($('#invoice_discount').val());
	discount=(discount/100)*total;
	$('#invoice_discount_amount').val(discount);
	total=total-discount;
	$('#invoice_subtotal').val(total);
	updateInvoiceTotals();
}
function updateInvoiceTotals(){
	var total=Number($('#invoice_subtotal').val());
	total+=Number($("#invoice_taxes").val());
	total-=Number($('#invoice_paid').val());
	$('#invoice_total').val(total);
}


function makeTableClickable(selector,callback){
	$(selector).click(function(){
		parent = $(this).parent();
		if(!parent.hasClass('row_selected')){
			parent.parent().children().removeClass('row_selected');
			parent.addClass('row_selected');
		}
	}).dblclick(function(){
		parent = $(this).parent();
		row_id = parent.attr('id');
		callback(row_id);
	});
}
$(function(){
	$('.chzn-select').chosen();

	$('#tabs').tabs({
		cookie: {
			expires: 1
		}
	});
	$('.datepicker').datepicker();

	$('.datetimepicker').datetimepicker({
		ampm:true,
		hourGrid:4,
		minuteGrid:15,
		stepHour:1,
		stepMinute:1,
		onSelect: function( selectedDate ) {
			var newDate = new Date(selectedDate);

			if(this.id=="timetrack_from"){
				$('#timetrack_to').datetimepicker("option",{minDate:newDate,minDateTime:newDate});
			}
			if(this.id=="timetrack_to"){
				$('#timetrack_from').datetimepicker("option",{maxDate:newDate,maxDateTime:newDate});
			}
		}
	});

	$('.row-rm-btn').button({
		icons: {
			primary: "ui-icon-trash"
		},
		text:false
	}).click(function(){
		$(this).parent().parent().remove();
	});

	$( "#timetrack-dialog-form" ).dialog({
		autoOpen: false,
		minWidth: 800,
		maxWidth: 800,
		width:800,
		modal: true,
		buttons: {
			"Save": function() {
	
			},
			Cancel: function() {
				$( this ).dialog( "close" );
			}
		},
		close: function() {

		}
	});

	$('#timetrack-new-btn').button({}).click(function(){
		$('#timetrack-dialog-form').dialog('open');
	});




	$("#timetrack_duration").countdown({
		since:new Date(),
		compact:true
	});
	$("#timetrack_duration").countdown('pause');
	var started=false;
	var playing=false
	$('#startTimer').button({
		icons: {
			primary: 'ui-icon-clock',
			secondary:'ui-icon-play'
		},
		text:false
	}).click(function(){
		if(!started){
			started=true;
			now=new Date();
			$("#timetrack_duration").countdown('change',{
				since: now
			});
			$("#timetrack_duration").countdown('pause');
			$('#timetrack_from').datetimepicker('setDate',now);
		}

		var options;
		if(playing){
			$("#timetrack_duration").countdown('pause');
			options = {
				icons:{
					primary:'ui-icon-clock',
					secondary:'ui-icon-play'
				},
				label: "Start Timer",
				text:false
			}
		} else {
			$("#timetrack_duration").countdown('resume');
			options = {
				icons:{
					primary:'ui-icon-clock',
					secondary:'ui-icon-pause'
				},
				label: "Pause Timer",
				text:false
			}
			$("#timetrack_to").datetimepicker('setDate',new Date());
		}
		playing=!playing;
		$(this).button('option',options);
	}).next().button({
		icons: {
			primary: 'ui-icon-refresh'
		},
		text:false
	}).click(function(){
		now=new Date();
		$("#timetrack_duration").countdown('change',{
			since: now
		});
		$('#timetrack_from').datetimepicker('setDate',now);
		$("#timetrack_duration").countdown('resume');
		$("#timetrack_duration").countdown('pause');
		$("#startTimer").button('option',{
				icons:{
					primary:'ui-icon-clock',
					secondary:'ui-icon-play'
				},
				label: "Start Timer",
				text:false
			});
		started=false;
	}).parent().buttonset();





	timetracks_dataTable = $('#timetracks_table').flexigrid({
		height: 200,
		themeRoller:true,
		singleSelect:true
	});
	
	makeTableClickable('#timetracks_table td',function(row_id){
		alert("Clicked "+row_id); //TODO: handle opening a timetrack item.
	});
});
